package com.example.playnote.fragment

import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.example.playnote.R
import com.example.playnote.activity.ui.TypeContentActivity
import com.example.playnote.adapter.TypeAdapter
import com.example.playnote.base.BaseFragment
import com.example.playnote.bean.Constant
import com.example.playnote.bean.ToastFlag
import com.example.playnote.bean.TreeListResponse
import com.example.playnote.databinding.FragmentTypeBinding
import com.example.playnote.toasty
import com.example.playnote.viewModel.HomeViewModel

class TypeFragment: BaseFragment<HomeViewModel, FragmentTypeBinding>() {
    private var datas: TreeListResponse? = null
    private val typeAdapter: TypeAdapter by lazy {
        TypeAdapter(requireContext(), datas)
    }
    private var isFirst: Boolean = true

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.typeSwipeRefreshLayout.run {
            isRefreshing = true
            setOnRefreshListener(onRefreshListener)
        }
        mDatabind.typeRecyclerView.run {
            layoutManager = LinearLayoutManager(activity)
            adapter = typeAdapter
        }
        typeAdapter.run {
            bindToRecyclerView(mDatabind.typeRecyclerView)
            setEmptyView(R.layout.fragment_home_empty)
            onItemClickListener = this@TypeFragment.onItemClickListener
        }
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden && isFirst) {
            mViewModel.getTypeTreeList()
            isFirst = false
        }
    }

    /**
     * refresh
     */
    fun refreshData() {
        mDatabind.typeSwipeRefreshLayout.isRefreshing = true
        mViewModel.getTypeTreeList()
    }

    /**
     * RefreshListener
     */
    private val onRefreshListener = SwipeRefreshLayout.OnRefreshListener {
        refreshData()
    }
    /**
     * ItemClickListener
     */
    private val onItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
        if (datas?.size != 0) {
            Intent(activity, TypeContentActivity::class.java).run {
                putExtra(Constant.CONTENT_TITLE_KEY, datas!![position].name)
                putExtra(Constant.CONTENT_CHILDREN_DATA_KEY, datas!![position])
                startActivity(this)
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.treeList.observe(this) {
            if (it.errorCode != 0) {
                getTypeListFailed(it.errorMsg)
                return@observe
            }
            if (it.data.isEmpty()) {
                getTypeListZero()
                return@observe
            }
            datas = it.data
            getTypeListSuccess(it.data)
        }
    }
    /**
     * scroll to top
     */
    fun smoothScrollToPosition() = mDatabind.typeRecyclerView.scrollToPosition(0)

    /**
     * get Type list Success
     */
    private fun getTypeListSuccess(result: TreeListResponse) {
        result.let {
            if (mDatabind.typeSwipeRefreshLayout.isRefreshing) {
                typeAdapter.replaceData(it)
            } else {
                typeAdapter.addData(it)
            }
        }
        mDatabind.typeSwipeRefreshLayout.isRefreshing = false
    }

    /**
     * get Type list Failed
     */
    private fun getTypeListFailed(errorMessage: String?) {
        requireContext().toasty(ToastFlag.ERROR, content = errorMessage?:getString(R.string.get_data_error))
        mDatabind.typeSwipeRefreshLayout.isRefreshing = false
    }

    /**
     * get Type list data size equal zero
     */
    private fun getTypeListZero() {
        requireContext().toasty(ToastFlag.ERROR, getString(R.string.get_data_zero))
        mDatabind.typeSwipeRefreshLayout.isRefreshing = false
    }


    override fun lazyLoadData() {
    }
}